using System;
using UnityEngine;

namespace NodeCanvas
{
	public static class TaskTools
	{
		public static string GetOperationString(OperationMethod om)
		{
			switch (om)
			{
			case OperationMethod.Set:
				return " = ";
			case OperationMethod.Add:
				return " += ";
			case OperationMethod.Subtract:
				return " -= ";
			case OperationMethod.Multiply:
				return " *= ";
			case OperationMethod.Divide:
				return " /= ";
			default:
				return string.Empty;
			}
		}

		public static object Operate(object a, object b, OperationMethod om)
		{
			Type type = a.GetType();
			if (type != b.GetType())
			{
				return a;
			}
			if (om == OperationMethod.Set)
			{
				return a = b;
			}
			if (type == typeof(float))
			{
				switch (om)
				{
				case OperationMethod.Add:
					return (float)a + (float)b;
				case OperationMethod.Subtract:
					return (float)a - (float)b;
				case OperationMethod.Multiply:
					return (float)a * (float)b;
				case OperationMethod.Divide:
					return (float)a / (float)b;
				}
			}
			if (type == typeof(int))
			{
				switch (om)
				{
				case OperationMethod.Add:
					return (int)a + (int)b;
				case OperationMethod.Subtract:
					return (int)a - (int)b;
				case OperationMethod.Multiply:
					return (int)a * (int)b;
				case OperationMethod.Divide:
					return (int)a / (int)b;
				}
			}
			if (type == typeof(Vector3))
			{
				switch (om)
				{
				case OperationMethod.Add:
					return (Vector3)a + (Vector3)b;
				case OperationMethod.Subtract:
					return (Vector3)a - (Vector3)b;
				case OperationMethod.Multiply:
					return Vector3.Scale((Vector3)a, (Vector3)b);
				case OperationMethod.Divide:
					return new Vector3(((Vector3)a).x / ((Vector3)b).x, ((Vector3)a).y / ((Vector3)b).y, ((Vector3)a).z / ((Vector3)b).z);
				}
			}
			Debug.LogError("Requested Operation with non compatible types");
			return a;
		}

		public static string GetCompareString(CompareMethod cm)
		{
			switch (cm)
			{
			case CompareMethod.EqualTo:
				return " == ";
			case CompareMethod.GreaterThan:
				return " > ";
			case CompareMethod.LessThan:
				return " < ";
			default:
				return string.Empty;
			}
		}

		public static bool Compare(IComparable a, IComparable b, CompareMethod cm, float floatingPoint = 0.05f)
		{
			switch (cm)
			{
			case CompareMethod.EqualTo:
				if (a.GetType() == typeof(float))
				{
					return Mathf.Abs((float)(object)a - (float)(object)b) <= floatingPoint;
				}
				return a == b;
			case CompareMethod.GreaterThan:
				return a.CompareTo(b) > 0;
			case CompareMethod.LessThan:
				return a.CompareTo(b) < 0;
			default:
				return true;
			}
		}
	}
}
